package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author np
 * @Date 2020/10/8 18:33
 * @Version 1.0
 * @Description: 权限
 */
@Service(interfaceClass = PermissionService.class)//要暴露服务一定要用dubbo的服务注解,加了事务注解之后就需要明确,当前服务实现的是哪一个接口
@Transactional
public class PermissionServiceImpl implements PermissionService {
    @Autowired//注入Dao对象,从spring工厂里查找对象然后注入
    private PermissionDao permissionDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();//ThreadLocal
        PageHelper.startPage(currentPage,pageSize);//分页插件，会在执行sql之前将分页关键字追加到SQL后面
        Page<Permission> page = permissionDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    @Override
    public void deleteById(Integer id) {
        long count = permissionDao.findCountByPermissionId(id);
        if (count > 0) {
            //当前检查项已经被关联到检查组,不允许被删除
            new RuntimeException();
        }
        //如果没有,调用删除方法删除
        //先定义删除方法接口,然后在映射文件编写删除的sql语句
        permissionDao.deleteById(id);
    }

    @Override
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }

    @Override
    public void edit(Permission permission) {
        permissionDao.edit(permission);
    }

    @Override
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }

}
